package cn.edu.swu.bc.servlet;

import cn.edu.swu.bc.filter.AuthFilter;
import cn.edu.swu.bc.utils.FileUtils;
import cn.edu.swu.bc.utils.JsonUtils;
import cn.edu.swu.bc.utils.DBUtils;
import cn.edu.swu.bc.entity.Blog;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class GetBlogFullTextServlet extends HttpServlet {

    private static final long serialVersionUID = 1198212124340L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("BID"));
        String pics=null;
        String title=null;
        String author=null;
        String time=null;
        Boolean isAdmin=(Boolean) request.getSession().getAttribute(AuthFilter.IS_ADMIN);
        Boolean isAuthor=false;
        List<String> blogFullText= new ArrayList<String>();
        try {
            blogFullText= FileUtils.getBlogContent(id,request);
            Blog blog= DBUtils.getBlogInfoByID(id);
            if(blog.getUID()==(Integer) request.getSession().getAttribute(AuthFilter.USER_ID))
               isAuthor=true ;
            pics=blog.getPic();
            title=blog.getTitle();
            author=blog.getAuthorname();
            time=blog.getDatetime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        Writer writer = response.getWriter();
        writer.write(JsonUtils.blogFullTextToJson(blogFullText,pics,author,title,time,(isAdmin||isAuthor)));
    }
}